<template>
  <el-container class="home-container">
    <el-header>
      <div>
        <img src="../assets/logo.png" alt="vue" height="100%"/>
        <span>电商后台管理系统</span>
      </div>
      <el-button type="info" @click="logout">退出</el-button>
    </el-header>
    <el-container>
      <el-aside width="200px">
        <div class="toggle-button" @click="toggleCollapse">|||</div>
        <!--侧边栏菜单区-->
        <el-menu background-color="black" text-color="white" active-text-color="green" :unique-opened="true"
                 :collapse="isCollapse" :collapse-transition="false" :router="true"
                 :default-active="activePath">
          <!--一级菜单-->
          <el-submenu :index="item.id+''" v-for="item in menulist" :key="item.id">
            <!--一级菜单模板区-->
            <template slot="title"><i :class="iconObj[item.id]"></i>
              <span>{{item.authName}}</span>
            </template>
            <!--index跳转地址-->
            <el-menu-item :index="'/'+subItem.path" v-for="subItem in item.children" :key="subItem.id"
                          @click="saveNavState('/'+subItem.path)">
              <template slot="title">
                <i class="el-icon-menu"></i>
                <span>{{subItem.authName}}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <el-main>
        <!--路由占位符-->
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
  export default {
    data () {
      return {
        menulist: [],
        iconObj: {
          1: 'el-icon-setting',
          2: 'el-icon-setting',
          3: 'el-icon-setting',
          4: 'el-icon-setting',
          14: 'el-icon-user-solid',
        },
        //是否折叠
        isCollapse: false,
        // 被激活链接地址
        activePath: ''
      }
    },
    created () {
      this.getMenuList()
      this.activePath = window.sessionStorage.getItem('activePath')
    },
    methods: {
      logout () {
        window.sessionStorage.clear()
        this.$router.push('/login')
      },
      //获取所有菜单
      async getMenuList () {
        const res = await this.$http.get('menus').then(
          res => {
            return res
          }
        ).catch(error => {
          console.log(error)
        })
        console.log(res)
        if (res.status !== 200) return this.$message.error(res.msg)
        this.menulist = res.data
        console.log(this.menulist)
      },
      toggleCollapse () {
        this.isCollapse = !this.isCollapse
      },
      saveNavState (activePath) {
        window.sessionStorage.setItem('activePath', activePath)
        this.activePath = activePath
      }
    }
  }
</script>

<style lang="less" scoped>
  .home-container {
    height: 100%;
  }

  .el-header {
    background-color: #333333;
    display: flex;
    justify-content: space-between;
    padding-left: 0px;
    align-items: center;
    color: white;
    font-size: 20px;

    > div {
      height: 100%;
      display: flex;
      align-items: center;

      span {
        margin-left: 15px;
      }
    }
  }

  .el-aside {
    background-color: #222222;
  }

  .el-main {
    background-color: ghostwhite;
  }

  .icon-font {
    margin-right: 10px;
  }

  .el-menu {
    border-right: 0px;
  }

  .toggle-button {
    background-color: #999999;
    font-size: 10px;
    line-height: 24px;
    text-align: center;
    color: white;
    //内容间距
    letter-spacing: 0.2em;
    //鼠标放上去小手
    cursor: pointer;
  }
</style>
